<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>编辑器</title>
  <style>
    .box {
      width: 500px;
      height: 300px;
      border: 1px solid;
    }
  </style>
</head>
<body>
  <button class="link">链接</button>
  <div class="box" contenteditable>
    <h1>一级标题</h1>
    <a href="https://www.baidu.com">百度链接</a>
    <p>这是一个段落</p>
  </div>

  <script>
    const $ = selector => document.querySelector(selector)

    $('.link').onclick = function() {
      document.execCommand('createLink', false, 'http://www.163.com')
    }
  </script>
</body>
</html>